package com.lfg.leetcode.leetcode100;


public class No134canCompleteCircuit {

    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int length = gas.length;
        int lessCnt = 0;
        int moreCnt = 0;
        int ret = 0;
        for (int index = 0; index < length; index++) {
            moreCnt += gas[index] - cost[index];
            if (moreCnt < 0) {
                ret = index + 1;
                lessCnt += moreCnt;
                moreCnt = 0;
            }
        }
        int total = lessCnt + moreCnt;
        if (total < 0) {
            return -1;
        }
        return ret;
    }
}
